package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.ArithUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStoragePickingMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStoragePickingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStoragePickingOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.serialNumber;

/**
 * Created by leij on 2017/6/6.
 */

public class RefundStoragePickingItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStoragePickingOverviewBean.OutputInfoBodyBean.ItemListBean> mRefundStorageItemList;
    private LayoutInflater inflater;
    private RefundStoragePickingOverviewActivity.RefundStoragePickingOverviewListener mRefundStoragePickingOverviewListener;
    private String mMaterielCode, mPickingNumber, mOutStorageNumber, mApplicationUnit, mOutStorageDate, mPickingApplicant, mPickingFactory, mMaterielType, mBusinessType, mReceiveFactory, mIsBuilding, mRemark;

    public RefundStoragePickingItemListAdapter(Context context, RefundStoragePickingOverviewBean.OutputInfoBodyBean data, List<RefundStoragePickingOverviewBean.OutputInfoBodyBean.ItemListBean> refundStorageItemList, String materielCode, RefundStoragePickingOverviewActivity.RefundStoragePickingOverviewListener refundStoragePickingOverviewListener) {
        mContext = context;
        mPickingNumber = data.getMat_req_code();
        mOutStorageNumber = data.getStock_output_code();
        mApplicationUnit = data.getApply_fty_name();
        mOutStorageDate = data.getCreate_time();
        mPickingApplicant = data.getUser_name();
        mPickingFactory = data.getMat_req_fty_name();
        mMaterielType = data.getMat_type_name();
        mBusinessType = data.getBiz_type_name();
        mReceiveFactory = data.getReceive_name();
        mIsBuilding = data.getIs_building_project();
        mRemark = data.getRemark();
        mRefundStorageItemList = refundStorageItemList;
        mRefundStoragePickingOverviewListener = refundStoragePickingOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mRefundStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mRefundStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_refund_storage_picking_item_list_first_item, null);
            TextView pickingNumber = (TextView) convertView.findViewById(R.id.pickingNumber);
            TextView outStorageNumber = (TextView) convertView.findViewById(R.id.outStorageNumber);
            TextView applicationUnit = (TextView) convertView.findViewById(R.id.applicationUnit);
            TextView outStorageDate = (TextView) convertView.findViewById(R.id.outStorageDate);
            TextView pickingApplicant = (TextView) convertView.findViewById(R.id.pickingApplicant);
            TextView pickingFactory = (TextView) convertView.findViewById(R.id.pickingFactory);
            TextView materielType = (TextView) convertView.findViewById(R.id.materielType);
            TextView businessType = (TextView) convertView.findViewById(R.id.businessType);
            TextView receiveFactory = (TextView) convertView.findViewById(R.id.receiveFactory);
            TextView isBuilding = (TextView) convertView.findViewById(R.id.isBuilding);
            EditText remark = (EditText) convertView.findViewById(R.id.remark);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            pickingNumber.setText(mPickingNumber);
            outStorageNumber.setText(mOutStorageNumber);
            applicationUnit.setText(mApplicationUnit);
            outStorageDate.setText(mOutStorageDate);
            pickingApplicant.setText(mPickingApplicant);
            pickingFactory.setText(mPickingFactory);
            materielType.setText(mMaterielType);
            businessType.setText(mBusinessType);
            receiveFactory.setText(mReceiveFactory);
            isBuilding.setText(mIsBuilding);
            remark.setText(mRemark);
            materielCode.setText(mMaterielCode);
            materielCode.setHint(mContext.getResources().getString(R.string.activity_in_storage_receive_overview_hint));
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mRefundStoragePickingOverviewListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mRefundStoragePickingOverviewListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            RefundStoragePickingOverviewBean.OutputInfoBodyBean.ItemListBean refundStorageItem = mRefundStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_refund_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, refundStorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.batchNumber = (TextView) view.findViewById(R.id.batchNumber);
        viewHolder.unit = (TextView) view.findViewById(R.id.unit);
        viewHolder.refundStorageNumberMax = (TextView) view.findViewById(R.id.refundStorageNumberMax);
        viewHolder.refundStorageNumber = (EditText) view.findViewById(R.id.refundStorageNumber);
        viewHolder.retainDecimal = (TextView) view.findViewById(R.id.retainDecimal);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStoragePickingOverviewBean.OutputInfoBodyBean.ItemListBean refundStorageItem) {
        viewHolder.serialNumber.setText(refundStorageItem.getRid());
        viewHolder.materielCode.setText(refundStorageItem.getMat_code());
        viewHolder.materielDescribe.setText(refundStorageItem.getMat_name());
        viewHolder.retainDecimal.setText(refundStorageItem.getDecimal_place());
        viewHolder.batchNumber.setText(TextUtils.isEmpty(refundStorageItem.getBatch()) ? refundStorageItem.getBatch_list().get(0).getBatch() : refundStorageItem.getBatch());
        viewHolder.unit.setText(refundStorageItem.getUnit_name());
        viewHolder.refundStorageNumberMax.setText(ArithUtils.sub(refundStorageItem.getOutput_qty(),refundStorageItem.getHave_return_qty()).toString());
        viewHolder.refundStorageNumber.setText(TextUtils.isEmpty(refundStorageItem.getHave_return_qty()) ? "" : refundStorageItem.getHave_return_qty());
        if (RefundStorageUtils.checkPickingItem(refundStorageItem)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }
        if (refundStorageItem.getBatch_list().size() > 1) {
            viewHolder.batchNumber.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));
            viewHolder.batchNumber.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    DialogUtils.getRefundStoragePickingBatchDialog(context, refundStorageItem.getBatch_list(), new DialogUtils.RefundStoragePickingBatchNumberListener() {
                        @Override
                        public void onSelect(Dialog dialog, String batchNumberStr, List<RefundStoragePickingOverviewBean.OutputInfoBodyBean.ItemListBean.BatchListBean.BatchSpecListBean> batchInfoList) {
                            viewHolder.batchNumber.setText(batchNumberStr);
                        }
                    }).show();
                }
            });
        } else {
            viewHolder.batchNumber.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
            viewHolder.batchNumber.setOnClickListener(null);
        }
        viewHolder.materielDescribe.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mRefundStoragePickingOverviewListener.onCheckData()) {
                    Intent intent = new Intent(context, RefundStoragePickingMaterielDetailActivity.class);
                    intent.putExtra(RefundStorageExtra.SERIAL, viewHolder.serialNumber.getText().toString());
                    context.startActivity(intent);
                }
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, batchNumber, unit, refundStorageNumberMax , retainDecimal;

        private EditText refundStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
